package qingke.app;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import qingke.pojo.AddLable;
import qingke.pojo.Lable;
import qingke.pojo.Memorandum;
import qingke.util.Console;

public class Dao {
	public void setLable(String name) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		Lable lable = new Lable();
		lable.setName(name);
		Transaction tx = session.beginTransaction();
		try {
			session.save(lable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public void setMemorandum(String title, String time, String content) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		Memorandum memorandum = new Memorandum();
		memorandum.setTitle(title);
		memorandum.setContent(content);
		memorandum.setCreateTime(time);
		Transaction tx = session.beginTransaction();
		try {
			session.save(memorandum);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public List<Lable> getLable() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<Lable> query = session.createQuery("from Lable", Lable.class);
		List<Lable> lables = query.getResultList();
		if (lables != null) {
			return lables;
		}
		session.close();
		return null;
	}

	public void updateLable(int id, String name) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Lable lable = session.get(Lable.class, id);
		lable.setName(name);
		Transaction tx = session.beginTransaction();
		try {
			session.update(lable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public List<Memorandum> getMemorandum() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<Memorandum> query = session.createQuery("from Memorandum", Memorandum.class);
		List<Memorandum> memorandums = query.getResultList();
		if (memorandums != null) {
			return memorandums;
		}
		session.close();
		return null;
	}

	public void updateMemorandum(int id, String title, String content) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Memorandum memorandum = session.get(Memorandum.class, id);
		memorandum.setTitle(title);
		memorandum.setContent(content);
		Transaction tx = session.beginTransaction();
		try {
			session.update(memorandum);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public void deleteMemorandum(int id) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Memorandum memorandum = session.get(Memorandum.class, id);

		Transaction tx = session.beginTransaction();
		try {
			session.delete(memorandum);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public void deleteLable(int id) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Lable lable = session.get(Lable.class, id);
		Transaction tx = session.beginTransaction();
		try {
			session.delete(lable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public void setAddLable(int id, int id1) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		Memorandum memorandum = session.get(Memorandum.class, id);
		Lable lable = session.get(Lable.class, id1);
		AddLable addLable = new AddLable();
		addLable.setLable(lable);
		addLable.setMemorandum(memorandum);
		Transaction tx = session.beginTransaction();
		try {
			session.save(addLable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public List<AddLable> getAddLable() {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<AddLable> query = session.createQuery("from AddLable", AddLable.class);
		List<AddLable> addLable = query.getResultList();
		if (addLable != null) {
			return addLable;
		}
		session.close();
		return null;
	}

	public void deleteAddLable(int id) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		AddLable addLable = session.get(AddLable.class, id);
		Transaction tx = session.beginTransaction();
		try {
			session.delete(addLable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public void updateAddLable(int id, int id1, int id2) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		AddLable addLable = session.get(AddLable.class, id);
		Memorandum memorandum = session.get(Memorandum.class, id1);
		Lable lable = session.get(Lable.class, id2);
		addLable.setLable(lable);
		addLable.setMemorandum(memorandum);
		Transaction tx = session.beginTransaction();
		try {
			session.update(addLable);
			tx.commit();
		} catch (HibernateException e) {
			e.printStackTrace();
			tx.rollback();
		}
		session.close();
	}

	public List<Memorandum> ListByContent(String content) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<Memorandum> query = session.createQuery(
				"from Memorandum where content = '" + content + "' order by create_time", Memorandum.class);
		// session.close();
		return query.getResultList();
	}

	public List<AddLable> ListByLable(int id) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		// Lable lable = session.get(Lable.class, id);

		Query<AddLable> query = session.createQuery("from AddLable where lable_id = "+id, AddLable.class);
		// session.close();
		return query.getResultList();
	}
//
//	public List<Memorandum> OrderByTime(int id) {
//		SessionFactory sf = new Configuration().configure().buildSessionFactory();
//		Session session = sf.openSession();
//		Query<Memorandum> query = session.createQuery(
//				"from Memorandum where id = (from AddLable where lable_id = " + id + ") order by create_time",
//				Memorandum.class);
//		return query.getResultList();
//	}
	public List<Memorandum> ListByTitle(String title) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();
		Query<Memorandum> query = session.createQuery(
				"from Memorandum where title = '" + title + "' order by create_time", Memorandum.class);
		// session.close();
		return query.getResultList();
	}
	
	public List<AddLable> ListByTwoLable(int id,int id1) {
		SessionFactory sf = new Configuration().configure().buildSessionFactory();
		Session session = sf.openSession();

		// Lable lable = session.get(Lable.class, id);

		Query<AddLable> query = session.createQuery("from AddLable where lable_id = "+id+"or lable_id ="+id1, AddLable.class);
		// session.close();
		return query.getResultList();
	}
}
